package com.myapp.ruyixing.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ConsumptionRecordList;
import com.myapp.ruyixing.model.ConsumptionRecordList.ConsumptionRecord;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class ConsumptionRecordActivity extends Activity {

	private ArrayList<ConsumptionRecordList.ConsumptionRecord> mConsumptionRecords;
	private ListView mListView;
	private ConsumptionRecordListAdapter mAdapter;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				ConsumptionRecordList consumptionRecordList = (ConsumptionRecordList) o;
				onLoadingDataFinished(true, consumptionRecordList);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consumption_record);
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
		String nickname = LoginHelper.getInstance().getNickName();
		if (!TextUtils.isEmpty(nickname)) {
			((TextView) findViewById(R.id.user_nickname)).setText(nickname);
		}
		mConsumptionRecords = new ArrayList<ConsumptionRecordList.ConsumptionRecord>();
		mAdapter = new ConsumptionRecordListAdapter();
		mListView = (ListView) findViewById(R.id.list);
		mListView.setAdapter(mAdapter);
		onLoadingDataStarted(LoginHelper.getInstance().getBDUSS(), 0, 20);
	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void onLoadingDataStarted(String bduss, int pn, int rn) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);
		RYHttpClient.getConsumptionRecordList(this, mHttpResponseHandler,
				bduss, pn, rn);
	}

	private void onLoadingDataFinished(boolean succeed,
			ConsumptionRecordList consumptionRecordList) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		if (succeed && consumptionRecordList != null) {
			if (consumptionRecordList.list != null
					&& consumptionRecordList.list.size() > 0) {
				mConsumptionRecords.clear();
				mConsumptionRecords.addAll(consumptionRecordList.list);
				mAdapter.notifyDataSetChanged();
			} else {
				showToast("您当前没有消费记录！");
			}
		} else {
			showToast("加载消费记录失败！");
		}
	}

	private class ConsumptionRecordListAdapter extends BaseAdapter {

		private class ViewHolder {
			TextView mShopName;
			TextView mState;
			TextView mName;
			TextView mPhone;
			TextView mDate;
			TextView mRemark;
		}

		@Override
		public int getCount() {
			return mConsumptionRecords == null ? 0 : mConsumptionRecords.size();
		}

		@Override
		public Object getItem(int arg0) {
			return mConsumptionRecords == null ? null : mConsumptionRecords
					.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			ConsumptionRecord record = (ConsumptionRecord) getItem(position);
			if (record == null) {
				return convertView;
			}
			if (convertView == null) {
				convertView = LayoutInflater.from(
						ConsumptionRecordActivity.this).inflate(
						R.layout.consumption_record_list_item, parent, false);
				holder = new ViewHolder();
				holder.mName = (TextView) (convertView
						.findViewById(R.id.text_name));
				holder.mDate = (TextView) (convertView
						.findViewById(R.id.text_time));
				holder.mShopName = (TextView) (convertView
						.findViewById(R.id.text_shop_name));
				holder.mPhone = (TextView) (convertView
						.findViewById(R.id.text_phone));
				holder.mRemark = (TextView) (convertView
						.findViewById(R.id.text_remark));
				holder.mState = (TextView) (convertView
						.findViewById(R.id.text_state));
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (!TextUtils.isEmpty(record.shopname)) {
				holder.mShopName.setText(record.shopname);
			}
			if (!TextUtils.isEmpty(record.content)) {
				holder.mRemark.setText(record.content);
			}
			if (!TextUtils.isEmpty(record.mobile)) {
				holder.mPhone.setText(record.mobile);
			}
			if (!TextUtils.isEmpty(record.name)) {
				holder.mName.setText(record.name);
			}
			if (!TextUtils.isEmpty(record.status)) {
				holder.mState.setText(record.status);
			}
			if (record.datetime >= 0) {
				holder.mDate.setText(DateFormat.format("yyyy-MM-dd hh:mm",
						record.datetime * 1000));
			}
			return convertView;
		}
	}

}
